package com.yztc.xiami_app.helper;

import android.app.Application;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.yztc.xiami_app.bean.ssq.Address;
import com.yztc.xiami_app.bean.ssq.SSQ;
import com.yztc.xiami_app.db.AddressManager;
import com.yztc.xiami_app.utils.PreUtils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.Bmob;
import cn.smssdk.SMSSDK;

/**
 * Created by 周旭 on 2016/11/18.
 */

public class MyApplication extends Application {
    //share
    {
        PlatformConfig.setWeixin("wx967daebe835fbeac", "5bb696d9ccd75a38c8a0bfe0675559b3");
        PlatformConfig.setSinaWeibo("3921700954", "04b48b094faeb16683c32669824ebdad");
        PlatformConfig.setQQZone("100424468", "c7394704798a158208a74ab60104f0ba");

    }

    //是否进入主画面key
    public static final String KEY_GOTO_MAIN = "KEY_GOTO_MAIN";
    @Override
    public void onCreate() {
        super.onCreate();
        UMShareAPI.get(this);//share
        initBmob();
        initSSQData();
        initMob();
    }

    //初始化Mob的短信验证码
    private void initMob() {
        SMSSDK.initSDK(this, "193ab8462960c", "464b91f706cd6cb4d749551766575c43");
    }

    //初始化Bmob云
    private void initBmob() {
        //Bmob默认初始化
        Bmob.initialize(this, "eb501c214a09b1fef037a4d12335306a");
    }
    private void initPush() {
        PushAgent mPushAgent = PushAgent.getInstance(this);
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {

            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回device token
                Log.i("tag",deviceToken);
            }

            @Override
            public void onFailure(String s, String s1) {

            }
        });
        //push自定义消息
        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler() {
            @Override
            public void dealWithCustomAction(Context context, UMessage msg) {
                Toast.makeText(context, msg.custom, Toast.LENGTH_LONG).show();
            }
        };
        mPushAgent.setNotificationClickHandler(notificationClickHandler);
    }

    //初始化省市区3级联动的数据
    private void initSSQData() {
        //检测，是否为第一次打开APP，如果是，则初始化一个省市区3级联动的数据库
        boolean isGotoMain = PreUtils.readBoolean(this, KEY_GOTO_MAIN);
        if (isGotoMain) {
            //不是第一次打开app
        } else {
            //第一次打开app
            AddressManager manager = new AddressManager(this);
            try {
                //初始化ssq数据库的数据
                manager.insert(getAddressesList());
            } catch (Exception e) {
                e.printStackTrace();
            }
            //设置用户的状态为：不是第一次使用的用户
            PreUtils.writeBoolean(this, KEY_GOTO_MAIN, true);
        }
    }

    //解析Json数据得到包含省市区地址的列表List
    @NonNull
    private List<Address> getAddressesList() {
        String ssqJson = getSsqJson();
        Gson gson = new Gson();
        SSQ ssq = gson.fromJson(ssqJson, SSQ.class);
        List<SSQ.ResultBean> resultBeanList = ssq.getResult();
        StringBuffer buffer = new StringBuffer();
        List<Address> addressList = new ArrayList<>();
        for (int i=0;i<resultBeanList.size();i++){
            String pName = resultBeanList.get(i).getName();
            List<SSQ.ResultBean.CityBean> cityBeen = resultBeanList.get(i).getCity();
            for (SSQ.ResultBean.CityBean city:cityBeen){
                String cityName = city.getName();
                List<String> areaList = city.getArea();
                buffer.append(pName).append(":").append(cityName).append("\n");
                for (int j=0;j<areaList.size();j++){
                    Address address = new Address(pName,cityName,areaList.get(j));
                    addressList.add(address);
                }
            }
        }
        return addressList;
    }

    //获取省市区的Json数据
    private String getSsqJson() {
        byte[] data = new byte[1024 * 8];
        int len;
        StringBuffer buffer = new StringBuffer();
        try {
            InputStream inputStream = getResources().getAssets().open("ssq.txt");
            while ((len = inputStream.read(data)) != -1){
                buffer.append(new String(data,0,len));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        return buffer.toString();
    }
}
